package project.dao.impl;

import Utils.db.dao.BaseDAO;
import project.dao.BookDAO;
import project.pojo.Book;

import java.sql.SQLException;
import java.util.List;

/**
 * @author d11yu
 */
public class BookDAOImpl extends BaseDAO implements BookDAO {
    @Override
    public int addBook(Book book) {
        String sql = "insert into t_book (name, price, author, sales, stock, img_path ) values (?,?,?,?,?,?)";
        return update(sql, book.getName(), book.getPrice(), book.getAuthor(), book.getSales(), book.getStock(), book.getImgPath());
    }

    @Override
    public int deleteBookById(Integer id) {
        String sql = "delete from t_book where id=?";
        return update(sql, id);
    }

    @Override
    public int deleteBookByName(String name) {
        String sql = "delete from t_book where name=?";
        return update(sql, name);
    }

    @Override
    public int updateBookByName(Book book) {
        String sql = "update t_book set name = ?, price = ?, author = ?, sales = ?, stock = ?, img_path = ? where name = ?";
        return update(sql, book.getName(), book.getPrice(), book.getAuthor(), book.getSales(), book.getStock(), book.getImgPath(), book.getName());
    }

    @Override
    public int updateBookById(Book book) {
        String sql = "update t_book set name = ?, price = ?, author = ?, sales = ?, stock = ?, img_path = ? where id = ?";
        return update(sql, book.getName(), book.getPrice(), book.getAuthor(), book.getSales(), book.getStock(), book.getImgPath(), book.getId());
    }

    @Override
    public int[] updateBooksById(List<Book> books) throws NoSuchFieldException, IllegalAccessException {
        String sql = "update t_book set name = ?, price = ?, author = ?, sales = ?, stock = ?, img_path = ? where id = ?";
        return updateBatch(sql, books, new String[]{"name", "price", "author", "sales", "stock", "imgPath", "id"});
    }

    @Override
    public int[] updateBooksSalesStockById(List<Book> books) throws NoSuchFieldException, IllegalAccessException {
        String sql = "update t_book set sales =?, stock = ? where id = ?";
        return updateBatch(sql, books, new String[]{"sales", "stock", "id"});
    }

    /**
     * @return
     * @throws SQLException
     */
    @Override
    public List<Book> queryAllBooks() throws SQLException {
        String sql = "select id, name, price, author, sales, stock, img_path as imgPath from t_book";
        return queryBeanList(Book.class, sql);
    }

    /**
     * @param name 书名
     * @return
     * @throws SQLException
     */
    @Override
    public Book queryBookByName(String name) throws SQLException {
        String sql = "select id, name, price, author, sales, stock, img_path as imgPath from t_book where name= ?";
        return queryBean(Book.class, sql, name);
    }

    @Override
    public Book queryBookById(Integer id) throws SQLException {
        String sql = "select id, name, price, author, sales, stock, img_path as imgPath from t_book where id= ?";
        return queryBean(Book.class, sql, id);
    }

    @Override
    public Integer getEntryCountTotal() throws SQLException {
        String sql = "select count(*) from t_book";
        return ((Number) queryScalar(sql)).intValue();
    }

    @Override
    public List<Book> queryBooksPerPage(int eStart, int ecp) throws SQLException {
        String sql = "select id, name, price, author, sales, stock, img_path from t_book limit ?, ?";
        return queryBeanList(Book.class, sql, eStart, ecp);
    }

    @Override
    public int getEntryCountTotalByPrice(double minPrice, double maxPrice) throws SQLException {
        String sql = "select count(*) from t_book where price between ? and ?";
        return ((Number) queryScalar(sql, minPrice, maxPrice)).intValue();
    }

    @Override
    public List<Book> queryBooksPerPageByPrice(double minPrice, double maxPrice, int eStart, int ecp) throws SQLException {
        String sql = "select id, name, price, author, sales, stock, img_path from t_book where price between ? and ? order by price limit ?, ?";
        return queryBeanList(Book.class, sql, minPrice, maxPrice, eStart, ecp);
    }
}

